<template>
  <div>
    <div class="header-abs" @click="handleClick">返回</div>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <span class="city">景点详情</span>
      <router-link to="/">
        <div class="back-icon">
          <span class="iconfont icon-back">&#xe624;</span>
        </div>
      </router-link>
    </div>
  </div>
</template>
<script>
export default {
  activated() {
    // 全局绑定事件 不解绑将出现大量bug
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleClick() {
      this.$router.push("/");
    },
    handleScroll() {
      let top = document.documentElement.scrollTop;

      if (top > 60) {
        let opacity = top / 140;
        opacity = opacity > 1 ? 1 : opacity;
        this.opacityStyle = {
          opacity: opacity
        };
        this.showAbs = false;
      } else {
        this.showAbs = true;
      }
    }
  },
  data() {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    };
  }
};
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.header-abs {
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 0.4rem;
  background-color: rgba(0, 0, 0, 0.5);
  color: #ffffff;
  line-height: 0.8rem;
  text-align: center;
}

.header-fixed {
  z-index: 2;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  line-height: 0.86rem;
  height: 0.86rem;
  color: #fff;
  background-color: $bgColor;
  text-align: center;

  .city {
    font-size: 0.32rem;
  }

  .back-icon {
    position: absolute;
    top: 0rem;
    left: 0;
    text-align: center;
    width: 0.64rem;
    font-size: 0.4rem;
    color: #fff;
  }
}
</style>
